﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using System.Data;
using System.Text;

namespace Catering.UserTraining.SlrClassTraining
{
    public partial class classtraining_list : UserIsNull
    {
        public string m_UserID = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] != null) m_UserID = Session["UserID"].ToString();
            if (!IsPostBack)
            {
                this.LoadDrop();
            }
        }

        protected void LoadDrop()
        {
            string userid = Session["PersonnelID"].ToString();
            int companyid = 0;
            int groupid = 0;
            int classid = 0;
            bool isAdmin = false;
            Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
            Bind_Data_to_Controls.Bind_Area_ddl(Drop_ZuoYeQu, isAdmin, groupid, companyid, true);
            Bind_Data_to_Controls.Bind_Class_ddl(Drop_ZuoYeQu.SelectedItem, Drop_BanZu, isAdmin, classid, true);
        }

        protected void Drop_ZuoYeQu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.Drop_ZuoYeQu.SelectedValue) || this.Drop_ZuoYeQu.SelectedValue == "-10")
            {
                this.Drop_BanZu.Items.Clear();
                this.Drop_BanZu.Items.Add(new ListItem("全部", "-10"));
                this.Drop_BanZu.SelectedValue = "-10";
                return;
            }
            DataTable dt_BanZu = new Catering_Rules.ClassInfo().GetList("AreaID=" + this.Drop_ZuoYeQu.SelectedValue).Tables[0];
            this.DropBind(this.Drop_BanZu, dt_BanZu, "id", "CName");
        }

        //公用的DropDownList绑定方法
        public void DropBind(DropDownList drop, DataTable dt, string strValue, string strText)
        {
            drop.Items.Clear();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItem item = new ListItem();
                    item.Text = dt.Rows[i][strText].ToString();
                    item.Value = dt.Rows[i][strValue].ToString();
                    drop.Items.Add(item);
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            this.LoadRep(this.GetStrWhere());
        }

        //绑定Rep控件
        public void LoadRep(string strwhere, int page)
        {
            this.AspNetPager1.RecordCount = new Catering_Rules.ClassLessons().GetRecordCount(strwhere);
            if (page > 0) this.AspNetPager1.CurrentPageIndex = page;
            DataTable dt_PurchaseInfo = new Catering_Rules.ClassLessons().GetList(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex - 1, strwhere + " order by LessonID desc").Tables[0];//分页处理
            this.Repeater1.DataSource = dt_PurchaseInfo;
            this.Repeater1.DataBind();
        }

        //绑定Rep控件
        public void LoadRep(string strwhere)
        {
            LoadRep(strwhere, 0);
        }

        //获取查询条件
        public string GetStrWhere()
        {
            StringBuilder sb = new StringBuilder(" 1=1 ");
            if (!string.IsNullOrEmpty(this.Drop_BanZu.SelectedValue) && this.Drop_BanZu.SelectedValue != "-10")
            {
                sb.Append(" and ClassID =" + this.Drop_BanZu.SelectedValue + " ");
            }

            if (!string.IsNullOrEmpty(this.txt_BookName.Text.Trim()))
            {
                if (this.Drop_SearchType.SelectedValue == "LessonName")
                {
                    sb.Append(" and LessonName like '%" + this.txt_BookName.Text.Trim() + "%' ");
                }
                else if (this.Drop_SearchType.SelectedValue == "LessonType")
                {

                }
            }
            return sb.ToString();
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            this.LoadRep(this.GetStrWhere());
        }


        /// <summary>
        /// 新增功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string url = "~/UserTraining/SlrClassTraining/classtraining_add.aspx?Flag=ADD";
            Response.Redirect(url);
        }

        protected void btn_ReturnIndex_Click(object sender, EventArgs e)
        {
            string url = "~/UserTraining/index.aspx";
            Response.Redirect(url);
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int LessonID = int.Parse(e.CommandArgument.ToString());
            if (LessonID <= 0) return;
            if (e.CommandName == "del")
            {
                if (new Catering_Rules.ClassLessons().Delete(LessonID))
                {
                    //删除资料
                    new Catering_Rules.ClassLessonsBooks().Delete(LessonID);
                    //删除学员登记
                    new Catering_Rules.ClassLessonsCheck().Delete(LessonID);
                    this.LoadRep(this.GetStrWhere());
                    ScriptManager.RegisterStartupScript(this, GetType(), "Affect", "alert('删除成功！')", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "Affect", "alert('删除失败！')", true);
                }
            }
            else if (e.CommandName == "edit")
            {
                string url = "~/UserTraining/SlrClassTraining/classtraining_add.aspx?Flag=EDIT&LessonID=" + LessonID;
                Response.Redirect(url);
            }
            else if (e.CommandName == "staff")
            {
                string url = "~/UserTraining/SlrClassTraining/classtraining_staff_register.aspx?LessonID=" + LessonID;
                Response.Redirect(url);
            }
            else if (e.CommandName == "books")
            {
                string url = "~/UserTraining/SlrClassTraining/classtraining_books_list.aspx?LessonID=" + LessonID;
                Response.Redirect(url);
            }
        }

        public string GetClassName(string strClassID)
        {
            Catering_Model.ClassInfo _ClassModel = new Catering_Rules.ClassInfo().GetModel(int.Parse(strClassID));
            if (_ClassModel == null) return "";
            return _ClassModel.CName;
        }

        public string GetLessionTypeName(string strLessionTypeID)
        {
            Catering_Model.LessonType _LessonTypeModel = new Catering_Rules.LessonType().GetModel(int.Parse(strLessionTypeID));
            if (_LessonTypeModel == null) return "";
            return _LessonTypeModel.LessonTypeName;
        }

        public string GetUpUserName(string strUserID)
        {
            Catering_Model.user _UserModel = new Catering_Rules.user().GetModel(int.Parse(strUserID));
            if (_UserModel == null) return "";
            return _UserModel.UName;
        }
    }
}